<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/28
 * Time: 9:49
 */

namespace app\admin\controller;
use app\admin\model\MemberUser;
use app\admin\model\StudyFile;
use app\admin\model\StudyLunbo;
use app\admin\model\StudyVideo;
use app\admin\model\StudyVideoOrder;

class Study extends Base
{
    public function shopImage(){
        $file =request()->file('file');
        $ret = $this->upload($file);
        return json($ret);
    }
    public function lunboList(){
        $lunboRes=StudyLunbo::order('lun_sort desc')->paginate(10);
        $this->assign('data',$lunboRes);
        return $this->fetch();
    }
    public function lunboAdd(){
        return $this->fetch();
    }
    public function lunboAddForm(){
        $post=input('post.');
        $data=[
            'lun_sort'=>$post['lun_sort'],
            'lun_image'=>$post['lun_image'],
            'lun_url'=>$post['lun_url']
        ];
        $save=StudyLunbo::insert($data);
        if ($save){
            return true;
        }
        return array('msg'=>'提交失败！');
    }
    public function lunboEdit(){
        $whereAA['lun_id']=input('get.lun_id');
        $lunboRes=StudyLunbo::where($whereAA)->find();
        $this->assign('data',$lunboRes);
        return $this->fetch();
    }
    public function lunboEditForm(){
        $post=input('post.');
        $data=[
            'lun_sort'=>$post['lun_sort'],
            'lun_image'=>$post['lun_image'],
            'lun_url'=>$post['lun_url']
        ];
        $where['lun_id']=$post['lun_id'];
        $save=StudyLunbo::where($where)->update($data);
        if ($save){
            return true;
        }
        return array('msg'=>'提交失败！');
    }
    public function lunboDel(){
        $post=input('post.');
        $count=count($post['data']);
        for($i=0;$i<$count;$i++){
            $whereAA['lun_id']=$post['data'][$i];
            $del=StudyLunbo::where($whereAA)->delete();
        }
        if($del){
            return array('msg'=>'success');
        }else{
            return array('msg'=>'error');
        }
    }
    //音视频列表
    public function videoList(){
        if(empty(input('get.type'))){
            $this->assign('type',0);
        }else{
            $this->assign('type',input('get.type'));
        }
        return $this->fetch();
    }
    public function videoListAjax($type,$page,$limit){
        $whereAA['video_state']=$type;
        $data= StudyVideo::where($whereAA)->page($page,$limit)->order('video_sort desc,id desc')->select();
        foreach($data as $k=>$v){
            $data[$k]['video_createtime']=date('Y-m-d H:i',$v['video_createtime']);
        }
        $count = StudyVideo::where($whereAA)->count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }
    //添加音视频
    public function videoAdd(){
        return $this->fetch();
    }
    public function videoAddFrom(){
        $post=input('post.');
        $data=[
            'video_sort'=>$post['video_sort'],
            'video_title'=>$post['video_title'],
            'video_logo'=>$post['video_logo'],
            'video_url'=>$post['video_url'],
            'video_price'=>$post['video_price'],
            'video_author'=>$post['video_author'],
            'video_remark'=>$post['video_remark'],
            'video_content'=>$post['video_content'],
            'video_createtime'=>time(),
            'video_groom'=>$post['video_groom'],
            'video_state'=>$post['video_state'],
            'video_show'=>$post['video_show'],

        ];
        $save=StudyVideo::insert($data);
        if($save){
            return true;
        }
        return json(array('msg'=>'提交失败！'));
    }
    //修改音视频
    public function videoEdit(){
        $whereAA['id']=input('get.id');
        $data=StudyVideo::where($whereAA)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function videoEditFrom(){
        $post=input('post.');
        $data=[
            'video_sort'=>$post['video_sort'],
            'video_title'=>$post['video_title'],
            'video_logo'=>$post['video_logo'],
            'video_url'=>$post['video_url'],
            'video_price'=>$post['video_price'],
            'video_author'=>$post['video_author'],
            'video_remark'=>$post['video_remark'],
            'video_content'=>$post['video_content'],
            'video_groom'=>$post['video_groom'],
            'video_state'=>$post['video_state'],
            'video_show'=>$post['video_show'],
        ];
        $whereAA['id']=$post['id'];
        $save=StudyVideo::where($whereAA)->update($data);
        if($save){
            return true;
        }
        return json(array('msg'=>'提交失败！'));
    }
    public function videoDel(){
        $post=input('post.');
        $count=count($post['id']);
        for($i=0;$i<$count;$i++){
            $whereAA['id']=$post['id'][$i];
            $del=StudyVideo::where($whereAA)->delete();
        }
        if($del){
            return json(array('code'=>'success','msg'=>'删除成功！'));
        }else{
            return json(array('msg'=>'删除失败！'));
        }
    }
    //下载文件列表
    public function fileList(){
        return $this->fetch();
    }
    public function fileListAjax($page,$limit){
        $data= StudyFile::page($page,$limit)->order('file_createtime desc')->select();
        foreach($data as $k=>$v){
            $data[$k]['file_createtime']=date('Y-m-d H:i:s',$v['file_createtime']);
        }
        $count = StudyFile::count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }
    public function fileAdd(){
        return $this->fetch();
    }
    public function fileAddFrom(){
        $post=input('post.');
        $data=[
            'file_title'=>$post['file_title'],
            'file_url'=>$post['file_url'],
            'file_groom'=>$post['file_groom'],
            'file_content'=>$post['file_content'],
            'file_createtime'=>time()
        ];
        $save=StudyFile::insert($data);
        if($save){
            return true;
        }
        return json(array('msg'=>'提交失败！'));
    }
    public function fileEdit(){
        $whereAA['id']=input('get.id');
        $data=StudyFile::where($whereAA)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function fileEditFrom(){
        $post=input('post.');
        $data=[
            'file_title'=>$post['file_title'],
            'file_url'=>$post['file_url'],
            'file_groom'=>$post['file_groom'],
            'file_content'=>$post['file_content']
        ];
        $whereAA['id']=$post['id'];
        $save=StudyFile::where($whereAA)->update($data);
        if($save){
            return true;
        }
        return json(array('msg'=>'提交失败！'));
    }
    public function fileDel(){
        $post=input('post.');
        $count=count($post['id']);
        for($i=0;$i<$count;$i++){
            $whereAA['id']=$post['id'][$i];
            $del=StudyFile::where($whereAA)->delete();
        }
        if($del){
            return json(array('code'=>'success','msg'=>'删除成功！'));
        }else{
            return json(array('msg'=>'删除失败！'));
        }
    }
    //音视频订单列表
    public function videoOrderList(){
        return $this->fetch();
    }
    public function videoOrderListAjax($page,$limit){
        $whereAA['order_pay_state']=1;
        $data= StudyVideoOrder::where($whereAA)->page($page,$limit)->order('id desc')->select();
        foreach($data as $k=>$v){
            $data[$k]['order_createtime']=date('Y-m-d H:i:s',$v['order_createtime']);
            $data[$k]['order_paytime']=date('Y-m-d H:i:s',$v['order_paytime']);

            $whereAB['id']=$v['video_id'];
            $data[$k]['video']=StudyVideo::where($whereAB)->find();

            $data[$k]['video_title']=$data[$k]['video']['video_title'];
            $data[$k]['video_logo']=$data[$k]['video']['video_logo'];
            $data[$k]['video_state']=$data[$k]['video']['video_state'];

            $whereAC['id']=$v['user_id'];
            $data[$k]['user']=MemberUser::where($whereAC)->find();

            $data[$k]['nick_name']=$data[$k]['user']['nick_name'];
            $data[$k]['user_image']=$data[$k]['user']['image'];
        }
        $count = StudyVideoOrder::where($whereAA)->count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }



}